package Tree;


/**
 * @Author: 吕庆龙
 * @Date: 2020/7/19 10:57
 * <p>
 * 功能描述:
 */
public class JZ_017 {

    //先序遍历树 A 中的每个节点rootA
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if (B == null || A == null) {
            return false;
        }
//        if (A.val == B.val && (helper(A.left, B.left) && helper(A.right, B.right))) {
//            return true;
//        }
        if (helper(A, B)) {
            return true;
        }
        //树B是树A左子树的子结构,树B是树A右子树的子结构
        return isSubStructure(A.left, B) || isSubStructure(A.right, B);
    }
    //判断树 A 中 以rootA为根节点的子树 是否包含树rootB
    private boolean helper(TreeNode rootA, TreeNode rootB) {
        if (rootB == null) {
            return true;
        }
        if (rootA == null) {
            return false;
        }
        if (rootA.val == rootB.val) {
            return helper(rootA.left, rootB.left) && helper(rootA.right, rootB.right);
        } else {
            return false;
        }
    }
}
